//
//  WatchViewController.m
//  SmartiOSPublisher
//
//  Created by Liu,Qingcan on 2017/5/1.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import "WatchViewController.h"
#import "UpdateCollectApi.h"

@interface WatchViewController ()
@property (nonnull,nonatomic,strong)UIWebView *webview;

@end

@implementation WatchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupWebview];
    [self setupNavBar];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)setupWebview{
    UIWebView *webview = [[UIWebView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:webview];
    NSURL *reurl = [NSURL URLWithString:self.url];
    NSURLRequest *request = [NSURLRequest requestWithURL:reurl];
    [webview loadRequest:request];
    
    self.title = @"中公教育";
    self.view.backgroundColor = [UIColor whiteColor];

}

- (void)setupNavBar{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    [defaults setBool:YES forKey:self.url];
    BOOL isCollect = [defaults boolForKey:self.url];
    if (isCollect) {
        UIBarButtonItem *stopItem = [[UIBarButtonItem alloc]initWithTitle:@"已收藏" style:UIBarButtonItemStyleDone target:self action:nil];
        self.navigationItem.rightBarButtonItems = @[stopItem];
    }else{
    UIBarButtonItem *stopItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStyleDone target:self action:@selector(collectBtnClicked:)];
//    UIBarButtonItem *resumeItem = [[UIBarButtonItem alloc]initWithTitle:@"继续" style:UIBarButtonItemStyleDone target:self action:@selector(resumeBtnClicked:)];
    self.navigationItem.rightBarButtonItems = @[stopItem];
    }
}


-(void)collectBtnClicked :(UIBarButtonItem *)button{
//    [button setTitle:@"已收藏" forState:UIControlStateNormal];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSInteger userId = [defaults integerForKey:@"userId"];
    
    UpdateCollectApi *api = [[UpdateCollectApi alloc]initWithUserId:userId roomId:self.roomId];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSDictionary *dict = request.responseJSONObject;
        NSNumber *status = dict[@"statusCode"];
        if (status.integerValue == 1) {
            NSLog(@"更新收藏记录成功");
            //插入收藏状态
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setBool:YES forKey:self.url];
//            [button setTitle:@"已收藏" forState:UIControlStateNormal];
            [button setTitle:@"已收藏"];
            
            
            
//            NSDictionary *dicts = dict[@"data"];
//            NSLog(@"获取用户信息成功");
        }
        
        
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        
    }];
    
}



@end
